﻿using Admin.CommonLib.Models.Account;
using Newtonsoft.Json;
using Swimj.CommonLib.Base;
using System.Net.Http.Json;

namespace Admin.RazorLib.Services
{
    public class AccountService : IAccountService
    {
        private readonly IHttpClientFactory _httpClientFactory;

        public AccountService(IHttpClientFactory httpClientFactory)
        {
            _httpClientFactory = httpClientFactory;
        }

        public async Task<Result<LoginResponseDto>> Login(LoginRequestDto requestDto)
        {

            var httpClient = _httpClientFactory.CreateClient("adminApi");
            var response = await httpClient.PostAsJsonAsync("/api/v1/accounts/login", requestDto);
            if (response.IsSuccessStatusCode)
            {
                var resultJson = await response.Content.ReadAsStringAsync();
                var loginResponseDto = JsonConvert.DeserializeObject<Result<LoginResponseDto>>(resultJson);

                return loginResponseDto;
            }

            return null;
        }
    }
}